<template>
	<view>
		<view style="background-color: #FFFFFF;">
			<cu-custom :isBack="true">
				<block slot="backText"></block>
				<block slot="content">{{corpCode}}-条码比对</block>
			</cu-custom>
		</view>
		<view class="cu-bar bg-white search fixed indexes" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input @confirm="getData" v-model="inputVal" type="text" placeholder="输入关键词" :focus="focus"></input>
			</view>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<view class="cu-bar bg-white solid-bottom margin-top " style="margin-top: 13%;">
			<view class="action text-xl">
				<text class="cuIcon-title text-orange"></text>比对结果
			</view>
		</view>
		<scroll-view scroll-y="true" class="indexes" :scroll-with-animation="true" :enable-back-to-top="true"
			:style="[{height:'calc(100vh - '+ CustomBar + 'px - 100px)'}]">
			<view class="bg-white margin-bottom">
				<view>
					<view class="flex-sub text-center solid-bottom text-md padding">
						<text class="text-black text-bold">SRM结果</text>
					</view>
				</view>
				<view class="text-gray cu-form-group">
					<text class="text-blue margin-right-xs">编码：{{srmviewCode}}</text>
				</view>
			</view>
			<view v-if="isShowe" class="action-section">
				<image src="../../static/dh.png" mode="aspectFit" style="width: 50px;height: 50px;"></image>
			</view>
			<view v-if="isShowo" class="action-section">
				<image src="../../static/cw.png" mode="aspectFit" style="width: 50px;height: 50px;"></image>
			</view>
			<view class="bg-white margin-bottom">
				<view>
					<view class="flex-sub text-center solid-bottom text-md padding">
						<text class="text-black text-bold">海外仓结果</text>
					</view>
				</view>
				<view class="text-gray cu-form-group">
					<text class="text-blue margin-right-xs">编码：{{hwcode}}</text>
				</view>
			</view>
		</scroll-view>

	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				focus: true,
				menuArrow: true,
				CustomBar: this.CustomBar, //滚动定位用
				inputVal: "", //扫码值
				cdata: '', //任务列表
				hwcode: '', //海外仓存货编码
				hwname: '', //海外仓存货名称
				srmviewCode: '', //srm存货编码
				srmcinvname: '', //srm存货名称
				posList: [],
				// isCheck: false, //是否可用
				isShowe: false,
				isShowo: false,
				vals: '',
				markCode: '', //装code变量
				supplierId:'',//供应商Id
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'auditlist', 'corpCode']),
		},
		onShow() {

		},
		onLoad(params) {
			this.markCode = JSON.parse(params.data).code
			this.supplierId = JSON.parse(params.data).supplierId || ''
		},
		methods: {
			/** 获取数据 扫描盘点任务单，获取当前盘点人需盘点货位列表*/
			getData() {
				this.cdata = '';
				this.focus = false; // 初始化 第二个输入框focus 属性
				setTimeout(() => {
					this.focus = true; //  第二个输入框获取焦点
				}, 500)
				// //1、标准条码前缀：srm码
				// if ((this.inputVal.indexOf("sc") > -1) || (this.inputVal.indexOf("dh") > -1) || (this.inputVal.indexOf(
				// 		"inv") > -1) || (this.inputVal.indexOf("po") > -1)) {
				// 	this.srmviewCode = this.inputVal
				// 	setTimeout(()=>{
				// 		this.inputVal = '';
				// 	},50)
				// 	this.getPostData();
				// 	return;
				// } else {
				// 	//2、客户的标签前缀（不是我们的标签就是客户的标签）：海外码
				// 	this.hwcode = this.inputVal
				// 	setTimeout(()=>{
				// 		this.inputVal = '';
				// 	},50)
				// 	this.getPostData();
				// 	return;
				// }
				
				uni.request({
					url: this.serverURL_a + `/barcode/decode/checkBarCode`,
					data: {
						barCode: this.inputVal,
						__sid: this.userInfo.sessionid,
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success: function(data) {
						let res = data.data
						if(res.result == 'true'){
							this.srmviewCode = this.inputVal
						}else{
							this.hwcode = this.inputVal
						}
						setTimeout(()=>{
							this.inputVal = '';
						},50)
						this.getPostData();
					}.bind(this),
					fail: function() {
						this.$api.msg('无法连接服务，请联系管理');
						this.inputVal = ''
						return;
					}.bind(this),
				})
				
				
			},

			/** 标准标签信息*/
			getPostData() {
				setTimeout(()=>{
					this.inputVal = '';
				},50)
				if (!this.srmviewCode || !this.hwcode) {
					this.$api.msg("请继续另一个码!");
					return;
				} else {
					uni.request({
						url: this.serverURL_a + '/so/App/checkSkuAndCode',
						data: {
							code: this.markCode,
							supplierId: this.supplierId,
							__sid: this.userInfo.sessionid,
							srmCode: this.srmviewCode,
							cusCode: this.hwcode,
						},
						header: {
							'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						},
						method: 'POST',
						success: function(res) {
							var result = res.data;
							if (result.result == 'true') {
								console.log("成功")
								//提示成功声
								console.log("=====testClick=====");
								let src = '../../static/cg1.mp3';
								//实例化声音  
								const Audio = uni.createInnerAudioContext();
								Audio.autoplay = true;
								Audio.src = src; //音频地址  
								Audio.play(); //执行播放  
								Audio.onError((res) => {
									console.log(res.errMsg);
									console.log(res.errCode);
								});
								Audio.onPause(function() {
									console.log('end');
									Audio.destroy();
								});
								setTimeout(()=>{
									this.inputVal = '';
								},50)
								this.cleanData();
							} else if (result.result == '1003') {
								console.log('end');
								this.$api.msg(result.message);
								console.log(result.message)
								console.log("SKU不存在！")
								console.log("=====testClick=====");
								let src = '../../static/bcz.mp3';
								//实例化声音  
								const Audio = uni.createInnerAudioContext();
								Audio.autoplay = true;
								Audio.src = src; //音频地址  
								Audio.play(); //执行播放  
								Audio.onError((res) => {
									console.log(res.errMsg);
									console.log(res.errCode);
								});
								Audio.onPause(function() {
									console.log('end');
									Audio.destroy();
								});
								setTimeout(()=>{
									this.inputVal = '';
								},50)
								this.cleanData();
								return;
							} else if (result.result == '1004') {
								this.$api.msg(result.message);
								console.log("请勿重复扫描！")
								console.log("=====testClick=====");
								let src = '../../static/chongfu.mp3';
								//实例化声音  
								const Audio = uni.createInnerAudioContext();
								Audio.autoplay = true;
								Audio.src = src; //音频地址  
								Audio.play(); //执行播放  
								Audio.onError((res) => {
									console.log(res.errMsg);
									console.log(res.errCode);
								});
								Audio.onPause(function() {
									console.log('end');
									Audio.destroy();
								});
								setTimeout(()=>{
									this.inputVal = '';
								},50)
								this.focus = true;
								this.cleanData();
								return;
							} else if (result.result == '1005') {
								this.$api.msg(result.message);
								console.log("数量超出！")
								console.log(result.message)
								console.log("=====testClick=====");
								let src = '../../static/slcc.mp3';
								//实例化声音  
								const Audio = uni.createInnerAudioContext();
								Audio.autoplay = true;
								Audio.src = src; //音频地址  
								Audio.play(); //执行播放  
								Audio.onError((res) => {
									console.log(res.errMsg);
									console.log(res.errCode);
								});
								Audio.onPause(function() {
									console.log('end');
									Audio.destroy();
								});
								setTimeout(()=>{
									this.inputVal = '';
								},50)
								setTimeout(() => {
									this.cleanData();
								}, 500)
								return;
							} else {
								this.$api.msg(result.message);
								console.log("扫码失败！")
								console.log(result.message)
								this.scSb();
								setTimeout(()=>{
									this.inputVal = '';
								},50)
								setTimeout(() => {
									this.cleanData();
								}, 500)
								return;
							}
						}.bind(this),
						fail: function() {
							this.$api.msg('服务已停止,请稍后再试');
							setTimeout(() => {
								this.inputVal = '';
								this.cleanData();
							}, 500)
							return;
						}.bind(this),
					});
				}
			},
			/** 扫码失败*/
			scSb() {
				console.log("=====testClick=====");
				let src = '../../static/smsb.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
				Audio.onPause(function() {
					console.log('end');
					Audio.destroy();
				});
			},
			/** 扫码成功*/
			scCg() {
				console.log("=====testClick=====");
				let src = '../../static/smcg.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
				Audio.onPause(function() {
					console.log('end');
					Audio.destroy();
				});
			},
			/** 清空数据 */
			cleanData() {
				this.srmviewCode = '';
				this.hwcode = '';
				this.focus = true;
			},
			/** 扫码*/
			toScan() {
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						var rdata = res.result;
						this.inputVal = rdata;
						setTimeout(function() {
							this.getData();
							this.inputVal = '';
							this.focus = true;
						}.bind(this), 100);
					}.bind(this)
				});
			},
		}
	}
</script>

<style>
	.confirm-btn {
		padding: 0 38upx;
		margin: 0;
		height: 76upx;
		line-height: 76upx;
		font-size: $font-base + 2upx;
		background: $uni-color-primary;
	}

	.page {
		height: 100Vh;
		width: 100vw;
	}

	.page.show {
		overflow: hidden;
	}

	.switch-sex::after {
		content: "\e716";
	}

	.switch-sex::before {
		content: "\e7a9";
	}

	.switch-music::after {
		content: "\e66a";
	}

	.switch-music::before {
		content: "\e6db";
	}

	.indexes {
		font-size: 18px;
	}

	/* 底部栏 */
	.action-section {
		position: fixed;
		left: 85%;
		display: flex;
		/* align-items: center; */
	}
</style>